package algotithm.interview.baiyu.test2;

import java.util.HashMap;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/25-21:00
 */
public class Solution {

    public static void main(String[] args) {
        int[] array = {2,1,1,2};
//        int[] array = {0,2,1,1};
        Solution solution = new Solution();
        int[] num = solution.sort(array);
        for (int i = 0; i < num.length; i++) {
            System.out.print(num[i] + " ");
        }
    }

    public int[] sort (int[] array) {
        // write code here
        int n = array.length;
        int[] num = new int[n];
        int count0 = 0;
        int count1 = 0;
        int count2 = 0;
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            map.put(array[i],map.getOrDefault(array[i],0) + 1);
        }
        if (map.containsKey(0)) {
             count0 = map.get(0);
        }
        if (map.containsKey(1)) {
             count1 = map.get(1);
        }
        if (map.containsKey(2)) {
             count2 = map.get(2);
        }
        for (int i = 0;i < count0; i++) {
            num[i] = 0;
        }
        for (int i = count0 ; i < count0 + count1; i++) {
            num[i] = 1;
        }
        for (int i = count0 + count1; i < n ; i++) {
            num[i] = 2;
        }
        return num;
    }

}
